<template>
	<view class="page">
		<cus-navbar bgColor="#f8f8f8">
			<block slot="right">
				<view class="search">
					<input v-model="searchKey" type="text" class="input" placeholder="请输入书名|作者" />
					<text class="text" @tap="searchHandle">搜索</text>
				</view>
			</block>
		</cus-navbar>
		<view v-if="!isSearched" class="searchKey">
			<view class="tag" v-for="(item,index) in keys" :key="index" @tap.stop.prevent="clickTag(item)">
				<text class="text">{{item}}</text>
				<image class="img" src="../../static/index/close.png" mode="scaleToFill"
					@tap.stop.prevent="deleteTag(index)">
				</image>
			</view>
		</view>
		<view v-else>
			<view class="novel" v-for="(item,index) in items" :key="item.id" @tap="novelTap(item)">
				<view class="content">
					<image class="default-cover cover" :data-cover="item.cover?'':'暂无封面'" :src="item.cover"
						mode="scaleToFill"></image>
					<view class="right">
						<view class="title">{{item.title}}</view>
						<view class="author-cat">
							{{item.author}}
							<text v-if="item.cat" class="split">·</text>
							{{item.cat}}
							<text v-if="item.wordCount" class="split">·</text>
							{{item.wordCount}}
						</view>
						<view v-if="item.lastChapter" class="desc">{{item.lastChapter}}</view>
						<view v-if="item.shortIntro" class="desc">{{item.shortIntro}}</view>
					</view>
				</view>
			</view>
			<view v-if="items.length===0" class="noList">暂无结果</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations
	}
	from 'vuex'
	export default {
		data() {
			return {
				searchKey: '',
				items: [],
				isSearched: false
			}
		},
		computed: {
			...mapGetters({
				'curSource': 'get_curSource',
				'keys': 'get_searchKey'
			}),
		},
		watch: {
			searchKey(nVal, oVal) {
				if (!nVal) {
					this.isSearched = false
					this.items = []
				}
			}
		},
		methods: {
			...mapMutations(['set_searchKey']),
			async getBookByKey() {
				uni.showLoading({
					title: '加载中'
				})
				let res = await this.$store.dispatch('getBookByKey', this.searchKey)
				uni.hideLoading()
				if (res) {
					this.items = res
				}
			},
			novelTap(item) {
				uni.navigateTo({
					url: '/pages/public/bookDetail?id=' + item._id
				})
			},
			addTag() {
				let index = this.keys.findIndex(item => item === this.searchKey)
				if (index === -1) {
					this.keys.push(this.searchKey)
					this.set_searchKey(this.keys)
				}
			},
			deleteTag(index) {
				this.keys.splice(index, 1)
				this.set_searchKey(this.keys)
			},
			clickTag(value) {
				this.isSearched = true
				this.searchKey = value
				this.getBookByKey()
			},
			searchHandle() {
				this.isSearched = true
				this.getBookByKey()
				this.addTag()
			}
		}
	}
</script>
<style lang="scss">
	.searchKey {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		flex-wrap: wrap;

		.tag {
			font-size: 12px;
			margin-top: 10px;
			margin-left: 10px;
			padding: 4px 8px;
			background-color: #f3f3f3;
			border-radius: 4px;

			display: flex;
			justify-content: flex-start;
			align-items: center;

			.img {
				width: 12px;
				height: 12px;
				margin-left: 10px;
			}
		}
	}

	.novel {
		padding: 30rpx;

		.content {
			display: flex;
			justify-content: flex-start;

			.cover {
				position: relative;
				width: 120rpx;
				height: 160rpx;
				border: 1px solid #E8E8E8;
				background-image: linear-gradient(to right bottom, rgba(0, 0, 255, 0.5), rgba(255, 0, 0, 0.5));

				&::after {
					content: "暂无封面";
					font-size: 11px;
					color: #525252;
					position: absolute;
					top: 50%;
					left: 50%;
					transform: translate(-50%, -50%);
				}
			}

			.right {
				width: 570rpx;
				padding-left: 30rpx;
				box-sizing: border-box;

				.title {
					// font-size: 28rpx;
					font-weight: bold;
				}

				.author-cat {
					color: #999;
					font-size: 26rpx;
					height: 40rpx;
					line-height: 40rpx;

					.split {
						padding: 0 16rpx;
					}
				}

				.desc {
					color: #999;
					font-size: 26rpx;
					height: 40rpx;
					line-height: 40rpx;

					overflow: hidden;
					text-overflow: ellipsis;
					word-break: break-all;
					display: -webkit-box;
					-webkit-line-clamp: 1;
					-webkit-box-orient: vertical;
				}
			}
		}
	}

	.novel+.novel {
		margin-top: 10rpx;
		border-top: 1px solid #f8f8f8;
	}

	.search {
		width: 100%;
		display: flex;
		justify-content: flex-start;
		align-items: center;

		.input {
			position: relative;
			width: 100%;
			background-color: #FFFFFF;
			padding: 4px 10px 4px 34px;
			border-radius: 20px;

			&::before {
				position: absolute;
				top: 50%;
				left: 2px;
				width: 30px;
				content: "\ea0e";
				display: block;
				font-size: 20px;
				font-family: uni;
				text-align: center;
				color: #cccccc;
				transform: translateY(-50%);
			}
		}

		.text {
			padding: 4px 10px;
			white-space: nowrap;
		}
	}

	.uni-input-placeholder {
		font-size: 15px;
		color: #cccccc;
	}

	.noList {
		text-align: center;
		margin-top: 100px;
	}
</style>
